package cn.edu.huanu.servletlearn;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;

/**
 * *.user采用后缀匹配，只关心后缀。注意：不能使用斜杠开头
 *
 * add.user 表示添加用户
 * delete.user 表示删除用户
 * update.user 表示修改用户
 *
 * @author hb
 * @date 2025-09-28 14:52:49
 */
@WebServlet("*.u")
public class UserManagerController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        String servletPath = request.getServletPath();
        String contextPath = request.getContextPath();

        System.out.println(requestURI);
        System.out.println(requestURL);
        System.out.println(servletPath);
        System.out.println(contextPath);
        //设置请求的字符编码
        request.setCharacterEncoding("utf-8");

        //获得请求数据包中请求头的消息
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            //获得请求的key
            String headerName = headerNames.nextElement();
            //获得key对应的value值
            String headerValue = request.getHeader(headerName);
            System.out.println(headerName+"="+headerValue);
        }
        //获得客户端的相关信息
        request.getHeader("user-agent");

        if("/add.user".equals(servletPath)){
            //执行添加用户的操作
            System.out.println("添加用户");
        }else if("/delete.user".equals(servletPath)){
            String id = request.getParameter("id");
            System.out.println("删除用户,id="+id);
        }
        else if("/update.user".equals(servletPath)){
            String id = request.getParameter("id");
            System.out.println("更新用户,id="+id);
        }
        else if("/query.user".equals(servletPath)){
            String keyword = request.getParameter("keyword");
            System.out.println("查询用户,keyword="+keyword);
        }

    }
}
